﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Device
{
    public partial class Login : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {           
            Image1.ImageUrl = "../WebForm1.aspx";
        }

        //登录
        protected void Button1_Click(object sender, EventArgs e)
        {
            string Str1 = "校验码错误！请重新输入！";
            string Str2 = "请获取验证码！";
            var checkCode = TextBox3.Text.Trim();
            RegionCode.Result result = RegionCode.CheckCodeSame(checkCode);
            switch (result)
            {
                case RegionCode.Result.Fail:
                    Util.AlertString(Str1, Response);
                    break;
                case RegionCode.Result.Success:
                    UserLogin();
                    break;
                case RegionCode.Result.CodeNull:
                    Util.AlertString(Str2, Response);
                    break;
            }
        }

        //取消
        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox1.Text = null;
            TextBox2.Text = null;
        }

        //换验证码
        protected void Button3_Click(object sender, EventArgs e)
        {
            Image1.ImageUrl = "../WebForm1.aspx";
        }

        //判断是哪个用户
        void UserLogin()
        {         
            if(DropDownList1 .Text.Trim () =="管理员")
            {
                DeviceMasterLogin ();
            }
            else 
            {
                AudienceLogin ();
            }
        }

        //判断观众用户名和密码是否一致
        void AudienceLogin()
        {
            string Str1 = "抱歉!您没有访问权限!";
            string Str2 = "抱歉!您的用户名和密码不匹配!";
            string password_sha = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text.Trim(), "SHA1");
            var email = TextBox1.Text.Trim();          
            Audiences.AuthResult authResult = Audiences.auth(email, password_sha);
            switch (authResult)
            {
                case Audiences.AuthResult.Lock:
                    Util.AlertString(Str1, Response);
                    break;
                case Audiences.AuthResult.Success:
                    Response.Redirect("Audience.aspx");
                    break;
                case Audiences.AuthResult.PasswordDoesnotMatch:
                    Util.AlertString(Str2, Response);
                    break;
            }
        }

        //判断设备主人用户与密码是否一致
        void DeviceMasterLogin()
        {
            string Str1 = "抱歉!您的用户名和密码不匹配!";
            string password_sha = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text.Trim(), "SHA1");
            var email = TextBox1.Text.Trim();
            DeviceMaster .AuthResult authResult=DeviceMaster .auth (email ,password_sha );
                switch(authResult )
                {
                    case  DeviceMaster.AuthResult.Login :
                        Session["loginUserid"] = DeviceMaster.CheckUserId(TextBox1.Text.Trim());
                        Response .Redirect ("DeviceMaster.aspx");
                        break;
                    case  DeviceMaster.AuthResult.PasswordDoesnotMatch :
                        Util.AlertString (Str1 ,Response);
                        break ;
                }
        }
            
    }
}